package com.sina.shop.api;

import com.sina.shop.domain.goods.GoodsComment;
import com.sina.shop.service.goods.GoodsCommentService;
import com.sina.shop.web.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * 段浩杰   2018-02-08-11:20
 */
@RestController
@RequestMapping("goodsComment")
public class GoodsCommentController extends BaseController {

    @Autowired
    @Qualifier("goodsCommentService")
    private GoodsCommentService goodsCommentService;

    @Autowired
    private DiscoveryClient discoveryClient;

    @RequestMapping(value = "/getGoodsCommentList", method = RequestMethod.GET)
    public Map<String, Object> getGoodsCommentList(GoodsComment goodsComment) {
        ServiceInstance instance = discoveryClient.getLocalServiceInstance();
        LOG.info("/getGoodsCommentList, method: get, host:" + instance.getHost() + ", serviceId:" + instance.getServiceId() + ",time: " + LocalDateTime.now());
        Map map = new HashMap(4);
        Long count[] = new Long[1];
        List<GoodsComment> list = goodsCommentService.getGoodsCommentList(goodsComment, count);
        map.put("count", count[0]);
        map.put("list", list);

        return map;
    }

    @RequestMapping(value = "/getGoodsComment", method = RequestMethod.POST)
    public GoodsComment getGoodsComment(Long id) {
        ServiceInstance instance = discoveryClient.getLocalServiceInstance();
        LOG.info("/getGoodsComment, method: post, host:" + instance.getHost() + ", serviceId:" + instance.getServiceId() + ",time: " + LocalDateTime.now());
        return goodsCommentService.getGoodsComment(id);
    }

    @RequestMapping(value = "/deleteGoodsComment", method = RequestMethod.POST)
    public Integer deleteGoodsComment(Long id) {
        ServiceInstance instance = discoveryClient.getLocalServiceInstance();
        LOG.info("/deleteGoodsComment, method: post, host:" + instance.getHost() + ", serviceId:" + instance.getServiceId() + ",time: " + LocalDateTime.now());
        return goodsCommentService.deleteGoodsComment(id);
    }

    @RequestMapping(value = "/saveGoodsComment", method = RequestMethod.POST)
    public GoodsComment saveGoodsComment(GoodsComment goodsComment) {
        ServiceInstance instance = discoveryClient.getLocalServiceInstance();
        LOG.info("/saveGoodsComment, method: post, host:" + instance.getHost() + ", serviceId:" + instance.getServiceId() + ",time: " + LocalDateTime.now());
        return goodsCommentService.saveGoodsComment(goodsComment);
    }

    @RequestMapping(value = "/updateGoodsComment", method = RequestMethod.POST)
    public GoodsComment updateGoodsComment(GoodsComment goodsComment) {
        ServiceInstance instance = discoveryClient.getLocalServiceInstance();
        LOG.info("/updateGoodsComment, method: post, host:" + instance.getHost() + ", serviceId:" + instance.getServiceId() + ",time: " + LocalDateTime.now());
        return goodsCommentService.updateGoodsComment(goodsComment);
    }
}
